import 'package:enterprise_v6_app/app/components/special_operation/custom_select_function.dart';
import 'package:enterprise_v6_app/app/components/toast_widget.dart';
import 'package:enterprise_v6_app/app/core/constans/dict_data_info.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_dict_all_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/special_list/violation_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/ticket_plan_entity.dart';
import 'package:get/get.dart';

class ViolationRecordsController extends GetxController {
  /// 作业票实体类
  TicketPlanReceiptLists ticketPlanReceiptLists = Get.arguments as TicketPlanReceiptLists;

  ///违规实体
  ViolationEntity violationEntity = ViolationEntity();

  /// 违规类型
  List<GetDictAllEntity> violationType = [];

  /// 选中的违规类型
  GetDictAllEntity? dictEntity;

  /// 回执信息
  List<ViolationViolationListDTOList> receiptList = [ViolationViolationListDTOList()];

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  void initData() async {
    // 获取违规类型字典
    violationType = await DictDataInfo.getDictionaryDataByTree(dictionaryType: 'wglx_TriuGKVU');
  }

  /// 类型选择
  void selectViolationType() {
    CustomSelectFunction.selectMultipleBottomSheet<GetDictAllEntity>(
      title: '类型选择',
      dataList: violationType,
      checkedList: [dictEntity ?? GetDictAllEntity()],
      isMultiple: false,
      onCallBack: (value) {
        dictEntity = value[0];
        violationEntity.violationTypeId = value[0].getId();
        violationEntity.violationTypeName = value[0].getShowName();
        update();
      },
    );
  }

  /// 添加回执
  void addReceipt() {
    receiptList.add(ViolationViolationListDTOList());
    AppToast.showSuccess('添加成功!');
    update();
  }

  /// 删除回执
  void removeReceipt(ViolationViolationListDTOList bean) {
    receiptList.remove(bean);
    update();
  }

  /// 确认
  Future<void> complete() async {
    // 交接内容
    if (violationEntity.violationTypeId!.isEmpty) {
      AppToast.showWarning('请选择违规类型');
      return;
    }
    // 交接内容
    if (violationEntity.violationReason!.isEmpty) {
      AppToast.showWarning('请输入违规理由');
      return;
    }
    // 填充接口数据
    violationEntity.ticketPlanId = ticketPlanReceiptLists.ticketPlanId;
    violationEntity.ticketReceiptId = ticketPlanReceiptLists.id;
    violationEntity.violationListDTOList = receiptList;

    final res = await HttpUtil()
        .generatePostRequest(
          Interface.addWorkViolation,
          data: violationEntity.toJson(),
        )
        .execute();
    res.onSuccess((result) {
      Get.back();
      AppToast.showWarning('登记成功');
    });
    res.onError((result) {
      AppToast.showWarning('登记失败');
    });
  }
}
